using model.entity;
using UnityEngine;

public class GunScript : MonoBehaviour
{
    public GunData gunData;
    public BulletData bulletData;
    
    private int currentAmmo;// 当前弹药数量
    private float nextFireTime;// 下一次射击时间
    private bool isReloading;// 是否正在装填
    
    private AudioSource audioPlayer;  // 音频播放器

    
    private void Start()
    {
        currentAmmo = gunData.magazineSize;
        // 获取或添加AudioSource组件
        audioPlayer = GetComponent<AudioSource>();
        if(audioPlayer == null) {
            audioPlayer = gameObject.AddComponent<AudioSource>();
        }
    }
    
    public void Initialize(string gunId)
    {
        gunData = GunManager.GetInstance().GetGun(gunId);
        bulletData = GunManager.GetInstance().GetBullet(gunData.bulletType);
        
        // 设置枪械精灵
        SpriteRenderer renderer = GetComponent<SpriteRenderer>();
        Debug.Log(renderer);
        Debug.Log(gunData.sprite);
        Debug.Log(gunData.sound);
        Debug.Log(bulletData.sprite);
        Debug.Log("播放器=============================");
        Debug.Log(audioPlayer);
        if (renderer != null && gunData.sprite != null)
        {
            renderer.sprite = gunData.sprite;
            //播放音效
            // AudioSource.PlayClipAtPoint(gunData.sound, Vector3.zero);
            audioPlayer.clip = gunData.sound;
            audioPlayer.Play();
        }
    }
    
    public void Fire(Vector2 position, Vector2 direction)
    {
        if (isReloading) return;
        
        if (Time.time >= nextFireTime)
        {
            if (currentAmmo > 0)
            {
                ShootBullet(position, direction);
                currentAmmo--;
                nextFireTime = Time.time + gunData.fireRate;
                
                // 播放射击音效
                if (gunData.sound != null)
                {
                    AudioSource.PlayClipAtPoint(gunData.sound, position);
                }
            }
            else
            {
                Reload();
            }
        }
    }
    
    private void ShootBullet(Vector2 position, Vector2 direction)
    {
        // 根据精准度添加随机偏移
        float accuracyOffset = (1 - gunData.accuracy) * 5f;
        Vector2 finalDirection = direction + Random.insideUnitCircle * accuracyOffset;
        finalDirection.Normalize();
        
        // 创建子弹
        GameObject bulletObj = new GameObject("Bullet");
        bulletObj.transform.position = position;
        
        // 添加并设置子弹组件
        BulletController bullet = bulletObj.AddComponent<BulletController>();
        bullet.Initialize(bulletData, finalDirection);
        
        // 设置子弹精灵
        SpriteRenderer renderer = bulletObj.AddComponent<SpriteRenderer>();
        renderer.sprite = bulletData.sprite;
        renderer.sortingOrder = 10;
        
        // 设置碰撞体
        CircleCollider2D collider = bulletObj.AddComponent<CircleCollider2D>();
        collider.radius = bulletData.size;
        collider.isTrigger = true;
    }
    
    public void Reload()
    {
        if (!isReloading && currentAmmo < gunData.magazineSize)
        {
            isReloading = true;
            Invoke("FinishReload", gunData.reloadTime);
        }
    }
    
    private void FinishReload()
    {
        currentAmmo = gunData.magazineSize;
        isReloading = false;
    }
    
    public int GetCurrentAmmo()
    {
        return currentAmmo;
    }
    
    public int GetMagazineSize()
    {
        return gunData.magazineSize;
    }
}